package com.healthcare;

import java.io.IOException;
import java.io.PrintWriter;

import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import webservices.HealthCareServerProxy;

/**
 * Servlet implementation class AddDoctorList
 */
public class AddDoctorList extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddDoctorList() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String user = request.getSession().getAttribute("userid").toString();
		System.out.println("user");
		String[] doctors = null;
		String inputSpecialization = request.getParameter("specialization");

		HealthCareServerProxy proxy = new HealthCareServerProxy();

		String doclist = proxy.getDocList(user, inputSpecialization);
		if (doclist != null) {
			doctors = doclist.split("&");
			System.out.println("doctors inside doclist adddoctorlist doget"
					+ doctors);
		}
		request.setAttribute("spec", inputSpecialization);
		System.out.println(inputSpecialization);
		RequestDispatcher dispatcher = request
				.getRequestDispatcher("/DocList.jsp");

		dispatcher.forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		if (request.getParameter("delete") != null) {

		}

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String inputDoctor = request.getParameter("doctorid");
		String inputUser = request.getParameter("userid");

		HealthCareServerProxy proxy = new HealthCareServerProxy();
		String status = proxy.doclist(inputDoctor, inputUser);
		if (status == "Doctor added to your list") {
			out.println("<html> <body>");
			out.println("<b><p>" + status + "</b></p>");
			out.println("</body> </html>");
		} else {
			out.println("<html> <body>");
			out.println("<b><p>" + status + "</b></p>");
			out.println("</body> </html>");
		}
		request.setAttribute("status", status);
		System.out.println(status);

	}

}
